import axios from 'axios'
import nProgress from 'nprogress'
import "nprogress/nprogress.css"
import store from "@/store"

const requsts = axios.create({
    // 配置对象
    baseURL:"/api",
    timeout:5000

})

// 请求拦截器
requsts.interceptors.request.use((config)=>{
    if(store.state.detail.uuid_token){
        config.headers.userTempId = store.state.detail.uuid_token
    }

    if(localStorage.getItem("TOKEN")){
        
    }
    // 需要携带token给服务器
    if(store.state.user.token){
        config.headers.token = store.state.user.token
    }

    nProgress.start()

    return config
})

// 响应拦截器
requsts.interceptors.response.use((res)=>{
    nProgress.done()

    // 响应成功的回调函数
    return res.data
},(error)=>{
    return Promise.reject(new Error('fail'))
})

export default requsts